public class MyLinkedlist {
    static class Listnode{
         public Listnode next;
         public Listnode prev;
         public int val;

         public Listnode(int val) {
             this.val = val;
         }
     }
    public Listnode last;
    public Listnode head;
    public void addFirst(int data){
        Listnode node = new Listnode(data);
       if(head==null){
           head=node;
           last = node;
       }
       else{
           node.next=head;
           node.next = head;
           head.prev = node;
           head= node;
       }
    }

    public void addLast(int data){
        Listnode node = new Listnode(data);
        if(head==null){
            head=node;
            last=node;
        }
        else{
            last.next = node;
            node.prev=last;
            last =node;
        }
    }

    public void addIndex(int index,int data){
        Listnode node = new Listnode(data);
        int len = size();
        if(index<0 || index>size()){
            return;
        }
        if(index==0){
            addFirst(data);
        }
        if(index==size()){
            addLast(data);
        }
        Listnode cur = head;
        int count =0;
        while(count<index){
            cur=cur.next;
            count++;
        }
        cur.prev.next = node;
        node.prev=cur.prev;
        node.next=cur;
        cur.prev = node;
    }
    public boolean contains(int key){
        Listnode cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    public int size(){
int count =0;
Listnode cur =head;
while(cur!=null){
    cur=cur.next;
            count++;
}
return count;
    }
}
